package com.zenith.edu.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.Result;
import com.zenith.edu.po.dto.EduQuestionDTO;
import com.zenith.edu.po.vo.EduQuestionVO;
import com.zenith.edu.service.EduQuestionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/edu/question")
@Tag(name = "题目管理", description = "题目管理接口")
public class EduQuestionController {
    
    @Autowired
    private EduQuestionService eduQuestionService;
    
    @SecurePostMapping(value = "/createQuestion", roles = "ROLE_ADMIN", permission = "question:create")
    @Operation(summary = "创建题目")
    public Result<EduQuestionVO> createQuestion(@RequestBody EduQuestionDTO dto) {
        EduQuestionVO questionVO = eduQuestionService.createQuestion(dto);
        return Result.success(questionVO);
    }
    
    @SecurePostMapping(value = "/getQuestionById/{id}", roles = "ROLE_ADMIN", permission = "question:read")
    @Operation(summary = "根据ID获取题目详情")
    public Result<EduQuestionVO> getQuestionById(@PathVariable Long id) {
        EduQuestionVO questionVO = eduQuestionService.getQuestionById(id);
        if (questionVO == null) {
            return Result.fail("题目不存在");
        }
        return Result.success(questionVO);
    }
    
    @SecurePostMapping(value = "/updateQuestion", roles = "ROLE_ADMIN", permission = "question:update")
    @Operation(summary = "更新题目信息")
    public Result<EduQuestionVO> updateQuestion(@RequestBody EduQuestionDTO dto) {
        EduQuestionVO questionVO = eduQuestionService.updateQuestion(dto);
        return Result.success(questionVO);
    }
    
    @SecurePostMapping(value = "/deleteQuestion/{id}", roles = "ROLE_ADMIN", permission = "question:delete")
    @Operation(summary = "删除指定题目")
    public Result<Void> deleteQuestion(@PathVariable Long id) {
        boolean result = eduQuestionService.deleteQuestion(id);
        if (!result) {
            return Result.fail("题目删除失败");
        }
        return Result.success();
    }
    
    @SecurePostMapping(value = "/getQuestionList", roles = "ROLE_ADMIN", permission = "question:read")
    @Operation(summary = "获取题目列表")
    public Result<List<EduQuestionVO>> getQuestionList() {
        List<EduQuestionVO> questionList = eduQuestionService.getQuestionList();
        return Result.success(questionList);
    }
}